/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mh.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.model.SelectItem;
import mh.bol.ManageEmployeeBO;
import mh.entity.EmployeeEn;

/**
 *
 * @author VIETNH
 */
@ManagedBean
@RequestScoped
public class AdminEmployeeRequest {

    ManageEmployeeBO meBO = new ManageEmployeeBO();
    ArrayList<EmployeeEn> listEmp = new ArrayList<EmployeeEn>();
    private int eid;
    private int did;
    private int etype;
    private String dname;
    private String userName;
    private String password;
    private String fullName;
    private String email;
    private String phone;
    private boolean gender;
    private String image;
    private String dateJoin;
    private boolean active;
    private String action;
    private EmployeeEn empEn;
    private String typeComp;

    public String getTypeComp() {
        return typeComp;
    }

    public void setTypeComp(String typeComp) {
        this.typeComp = typeComp;
    }

    /** Creates a new instance of AdminEmployeeRequest */
    public AdminEmployeeRequest() {
        checkSession();
        listEmp = meBO.getAllEmployee();
    }

    public String viewAllEmployee() {
        listEmp = meBO.getAllEmployee();
        return "AdListEmployee.xhtml";
    }

    public List listDepartment() {
        List selectItems = new ArrayList();
        selectItems.add(new SelectItem("1", "Educational Services"));
        selectItems.add(new SelectItem("2", "Management Services"));
        selectItems.add(new SelectItem("3", "Learning Services"));
        selectItems.add(new SelectItem("4", "Internal Systems"));
        selectItems.add(new SelectItem("5", "Human Resources"));
        return selectItems;
    }

    public List listTypeComp() {
        List selectItems = new ArrayList();
        selectItems.add(new SelectItem("1", "Facilities"));
        selectItems.add(new SelectItem("2", "Wage"));
        selectItems.add(new SelectItem("3", "Other issues"));
        return selectItems;
    }

    public List listTypeEmp() {
        List selectItems = new ArrayList();
        selectItems.add(new SelectItem("1", "Admin"));
        selectItems.add(new SelectItem("2", "Tecnician"));
        selectItems.add(new SelectItem("3", "Employee"));
        return selectItems;
    }

    public void checkSession() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ValueBinding binding = facesContext.getApplication().createValueBinding("#{loginSession}");
        LoginSession loginSession = (LoginSession) binding.getValue(facesContext);
        if (loginSession.getEmpEn() != null) {
            if (loginSession.getEmpEn().getEtype() == 3) {
                redirect("index.xhtml");
            }
        } else {
            redirect("Login.xhtml");
        }
    }

    public String logout() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ValueBinding binding = facesContext.getApplication().createValueBinding("#{loginSession}");
        LoginSession loginSession = (LoginSession) binding.getValue(facesContext);
        loginSession.resetSession();
        return "Login.xhtml";
    }

    public String actionEmployee() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
        if (requestMap.get("eID") != null && requestMap.get("action").toString() != null) {
            eid = Integer.parseInt(requestMap.get("eID").toString());
            action = requestMap.get("action").toString();
        }
        empEn = createEmployee();
        System.out.println("EID : " + empEn.getEid());
        System.out.println("DID : " + empEn.getDid());
        if (meBO.actionEmployee(action, empEn)) {
            redirect("AdListEmployee.xhtml");
            return "AdListEmployee.xhtml";
        } else {
            return "error.xhtml";
        }

    }

    public String deleteEmployee() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
        if (requestMap.get("eID") != null && requestMap.get("action").toString() != null) {
            eid = Integer.parseInt(requestMap.get("eID").toString());
        }
        if (meBO.deleteEmployee(eid + "")) {
            redirect("AdListEmployee.xhtml");
            return "AdListEmployee.xhtml";
        } else {
            return "error.xhtml";
        }

    }

    public void redirect(String page) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(page);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public EmployeeEn createEmployee() {
        EmployeeEn e = new EmployeeEn();
        e.setActive(active);
        e.setDateJoin(dateJoin);
        e.setDid(did);
        e.setDname(dname);
        e.setEid(eid);
        e.setEmail(email);
        e.setFullName(fullName);
        e.setGender(gender);
        e.setImage(image);
        e.setPassword(password);
        e.setPhone(phone);
        e.setUserName(userName);
        e.setEtype(etype);
        return e;
    }

    public void createProperty(EmployeeEn e) {
        active = e.isActive();
        etype = e.getEtype();
        dateJoin = e.getDateJoin();
        did = e.getDid();
        dname = e.getDname();
        eid = e.getEid();
        email = e.getEmail();
        fullName = e.getFullName();
        gender = e.isGender();
        image = e.getImage();
        password = e.getPassword();
        phone = e.getPhone();
        userName = e.getUserName();
    }

    public String viewEmployeeByID() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
        eid = Integer.parseInt(requestMap.get("eID").toString());
        empEn = meBO.viewDetailEmployee(eid + "");
        createProperty(empEn);
        return "AdDetailEmployee.xhtml";
    }

    public EmployeeEn getEmpEn() {
        return empEn;
    }

    public void setEmpEn(EmployeeEn empEn) {
        this.empEn = empEn;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public int getEtype() {
        return etype;
    }

    public void setEtype(int etype) {
        this.etype = etype;
    }

    public String getDateJoin() {
        return dateJoin;
    }

    public void setDateJoin(String dateJoin) {
        this.dateJoin = dateJoin;
    }

    public int getDid() {
        return did;
    }

    public void setDid(int did) {
        this.did = did;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public boolean isGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public ArrayList<EmployeeEn> getListEmp() {
        return listEmp;
    }

    public void setListEmp(ArrayList<EmployeeEn> listEmp) {
        this.listEmp = listEmp;
    }
}
